package com.jd.user.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.crypto.SecureUtil;
import com.jd.basic.exception.BusinessException;
import com.jd.basic.service.impl.BaseServiceImpl;
import com.jd.basic.util.AjaxResult;
import com.jd.user.domain.Logininfo;
import com.jd.user.domain.User;
import com.jd.user.mapper.LogininfoMapper;
import com.jd.user.mapper.UserMapper;
import com.jd.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * 业务实现类：
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LogininfoMapper logininfoMapper;
    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public void add(User user) {
        // 添加处理 对密码盐值处理
        String salt = RandomUtil.randomString(32);
        String md5 = SecureUtil.md5(user.getPassword() + salt);
        user.setPassword(md5);
        user.setSalt(salt);
        // 创建一个logininfo
        Logininfo logininfo = new Logininfo();
        // 赋值
        BeanUtils.copyProperties(user, logininfo);
        logininfo.setType(1); // 类型为员工
        logininfoMapper.add(logininfo); // 获取自增长id
        user.setLogininfoId(logininfo.getId());
        super.add(user);
    }

    @Override
    public void update(User user) {
        // 修改密码盐值loginId不进行改变
        User oldUser = userMapper.findOne(user.getId());
        user.setSalt(oldUser.getSalt());
        user.setPassword(oldUser.getPassword());
        user.setLogininfoId(oldUser.getLogininfoId());
        // 同步信息
        Logininfo oldLoginInfo = logininfoMapper.findOne(oldUser.getLogininfoId());
        BeanUtils.copyProperties(user, oldLoginInfo);
        oldLoginInfo.setId(oldUser.getLogininfoId());
        logininfoMapper.update(oldLoginInfo);
        super.update(user);
    }

    @Override
    public void del(Long id) {
        // 同步删除
        User user = userMapper.findOne(id);
        super.del(id);
        logininfoMapper.del(user.getLogininfoId());
    }

    @Override
    public void patchDel(Long[] ids) {
        // 同步批量删除
        for (Long id : ids) {
            del(id);
        }
    }

    @Override
    public AjaxResult registerPhone(Map<String, String> map) { // 手机注册
        // 空值判断
        String phone = map.get("phone");
        String phoneCode = map.get("phoneCode");
        String password = map.get("password");
        String passwordRepeat = map.get("passwordRepeat");
        if (StrUtil.isBlank(phoneCode) || StrUtil.isBlank(phone) || StrUtil.isBlank(password) ||
                StrUtil.isBlank(passwordRepeat)
        ) {
            throw new BusinessException("信息不能为空！");
        }
        // 手机验证码校验
        // 判断是否存在
        Object phoneCodeObj = redisTemplate.opsForValue().get("register:" + phone);
        if (phoneCodeObj == null){ // 不存在已过期
            throw new BusinessException("手机验证码已过期！");
        }
        // 判断是否正确
        if (!phoneCodeObj.toString().split(":")[0].equals(phoneCode)){
            throw new BusinessException("手机验证码错误！");
        }
        // 密码一致性判断
        if (!passwordRepeat.equals(password)){
            throw new BusinessException("两次密码输入不正确！");
        }
        // 操作数据库
        User user = new User();
        // 添加值
        user.setUsername(phone);
        user.setPhone(phone);
        // 对密码盐值处理
        String salt = RandomUtil.randomString(32);
        String md5 = SecureUtil.md5(password + salt);
        user.setSalt(salt);
        user.setPassword(md5);
        user.setState(1); // 启用
        // 同步logininfo
        Logininfo logininfo = new Logininfo();
        BeanUtils.copyProperties(user,logininfo);
        logininfo.setType(1); // 用户
        logininfo.setDisable(true); // 启用
        logininfoMapper.add(logininfo); // 获取id
        user.setLogininfoId(logininfo.getId());
        userMapper.add(user);
        return new AjaxResult();
    }
}
